package com.meilai.project.controller.web.administration.goods;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.administration.goods.GoodsPutStorageDTO;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.service.administration.goods.GoodsPutStorageService;
import com.meilai.project.vo.administration.goods.GoodsOutStorageVO;
import com.meilai.project.vo.business.sale.BillSaleGroupVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "物品入库管理", tags = {"入库管理"})
@RestController
@RequestMapping("api/web/goods_put_storage_manage")
public class GoodsPutStorageController {

    @Autowired
    private GoodsPutStorageService service;


    @GetMapping("list")
    @ApiOperation(value = "入库单列表")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                             @ApiParam(name = "group_id", value = "分组ID", required = false)
                             @RequestParam(value = "group_id", required = false) Long group_id,
                             @ApiParam(name = "goods_type_id", value = "物品类型ID", required = false)
                             @RequestParam(value = "goods_type_id", required = false) Long goods_type_id,
                             @ApiParam(name = "user_id", value = "入库人ID", required = false)
                             @RequestParam(value = "user_id", required = false) Long user_id) {
        GoodsPutStorageDTO searchDTO = new GoodsPutStorageDTO();
        searchDTO.setUser_id(user_id);
        searchDTO.setGoods_type_id(goods_type_id);
        searchDTO.setGroup_id(group_id);
        return new CommonResult().success(service.selectPageList(page, page_size, searchDTO));
    }

    @GetMapping("/group_list")
    @ApiOperation("列表待选项")
    public ApiResult<List<BillSaleGroupVO>> getList() {
        return ApiResult.ok(service.getList());
    }


//    @ApiOperation(value = "删除", notes = "删除")
//    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
//            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
//            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
//            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
//            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
//    @ResponseBody
//    @DeleteMapping("delete")
//    public CommonResult delete(@RequestParam(value = "id", required = true) Long id) {
//        try {
//            if (service.delete(id)) {
//                return new CommonResult().success("删除成功");
//            } else {
//                return new CommonResult().failed("删除失败");
//            }
//        } catch (Exception e) {
//            return new CommonResult().failed(e.getMessage());
//        }
//    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult add(@RequestBody GoodsPutStorageDTO dto) {
        try {
            if (service.addOne(dto)) {
                return new CommonResult().success("新增成功");
            }
            return new CommonResult().failed("新增失败");
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "详情", notes = "详情", response = GoodsOutStorageVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("detail/{id}")
    public CommonResult detail(@ApiParam(name = "id", value = "数据id", required = true) @PathVariable("id") Long id) {
        return new CommonResult().success(service.getOneById(id));
    }

//    @PostMapping("edit")
//    @ApiOperation(value = "修改")
//    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
//            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
//            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
//            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
//            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
//    @ResponseBody
//    public CommonResult edit(@RequestBody GoodsPutStorageDTO dto) {
//        if (service.editOne(dto)) {
//            return new CommonResult().success("修改成功");
//        }
//        return new CommonResult().failed("修改失败");
//    }

}
